package com.fix.web;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fix.dao.AuthDao;
import com.fix.dao.UserDao;
import com.fix.dao.impl.AuthDaoImpl;
import com.fix.dao.impl.UserDaoImpl;
import com.fix.entity.User;

/**
 *  用户登录
 */
@WebServlet("/userlogin")
public class LoginServlet extends HttpServlet {
	
	UserDao userDao =new UserDaoImpl();
	AuthDao authDao = new AuthDaoImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uname = request.getParameter("stname");
		String upass = request.getParameter("stpass");
		
		try {
			User user =userDao.login(uname, upass);
			if(user == null) { //登录失败
				response.sendRedirect("/auth/login.jsp");
			}else {
				//成功 去首页
				// session 放登录标志， 该用户的权利列表
				HttpSession session =request.getSession();						
				List userAuths =authDao.getAuthByUserid(user.getUid());
				session.setAttribute("LOGINUSER", user);
				session.setAttribute("userAuths", userAuths);
				//首页上显示所有的权利 
				List auths = authDao.getAuthList();
				request.setAttribute("auths", auths);
				request.getRequestDispatcher("/index.jsp").forward(request, response);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
	}

	 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
